package com.nand.module.function.ui;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.test.internal.util.LogUtil;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.PropertyPreFilter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.lzy.okgo.model.HttpParams;
import com.nand.library.base.base.BaseActivity;
import com.nand.library.base.bean.ResponseBean;
import com.nand.library.base.http.MyCallback;
import com.nand.library.base.http.OkGoUtils;
import com.nand.library.base.utils.SpUtils;
import com.nand.module.function.R;
import com.nand.module.function.adapter.EquipmentAdapter;
import com.nand.module.function.adapter.MMMDetailsAdapter;
import com.nand.module.function.bean.MMMBean;
import com.nand.module.function.bean.MMMDetailBean;
import com.nand.module.function.bean.PollingBean;
import com.nand.module.function.constant.URL;
import com.nand.module.function.utils.LogUtils;
import com.nand.module.function.utils.SpacesItemDecoration;
import com.nand.module.function.utils.ToolUtils;
import com.nand.module.function.view.CommonPopupWindow;

import java.util.ArrayList;
import java.util.List;

/**
 * created by 86185
 * on 2020/1/16
 */
@Route(path = "/function/mmmDetails")
public class MMMDetailsActivity extends BaseActivity {

    private RecyclerView mRecycler;
    private TextView mTvDjzt;
    private TextView mTvGz;
    private TextView mTvDjbh;
    private int oid;
    private MMMDetailsAdapter mRecyclerAdapter;
    private int type;
    private CommonPopupWindow.LayoutGravity layoutGravity;
    private CommonPopupWindow windowContent;
    private TextView horiText;
    private int userId;

    @Override
    protected int initLayout() {
        return R.layout.activity_mmm_details;
    }
    @Override
    protected boolean isActionBar() {
        return true;
    }


    @Override
    protected void initView() {
        mActionBar().setCenterText("物料移库看板");
        userId = SpUtils.readInt(this, "userId", 0);
        oid = getIntent().getIntExtra("oid", 0);
        type = getIntent().getIntExtra("type", 1);
        mTvDjbh = findViewById(R.id.tv_djbh);
        mTvGz = findViewById(R.id.tv_gz);
        mTvDjzt = findViewById(R.id.tv_djzt);
        mRecycler = findViewById(R.id.recycler);

        mRecycler.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerAdapter = new MMMDetailsAdapter(MMMDetailsActivity.this, type);
        //mRecycler.addItemDecoration(new DividerGridItemDecoration(this, 18, R.color.color_f4));
       /* int space = 20;
        mRecycler.addItemDecoration(new SpacesItemDecoration(space));*/
        mRecycler.setAdapter(mRecyclerAdapter);

        switch (type){
            case 1:
                findViewById(R.id.tv_confirm).setVisibility(View.VISIBLE);
                break;
            case 2:
                findViewById(R.id.tv_confirm).setVisibility(View.GONE);
                break;
        }

        initPop();
        mRecyclerAdapter.setOnItemClickListener(new MMMDetailsAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {

            }

            @Override
            public void onWlbmClick(int position, String context, TextView a) {
                layoutGravity=new CommonPopupWindow.LayoutGravity(CommonPopupWindow.LayoutGravity.ALIGN_LEFT| CommonPopupWindow.LayoutGravity.TO_BOTTOM);
                layoutGravity.setHoriGravity(CommonPopupWindow.LayoutGravity.ALIGN_RIGHT);
                layoutGravity.setVertGravity(CommonPopupWindow.LayoutGravity.TO_BOTTOM);
                windowContent.showBashOfAnchor(a, layoutGravity, 0, 0);
                horiText.setText(context);
            }

            @Override
            public void onWlmcClick(int position, String context, TextView a) {
                layoutGravity=new CommonPopupWindow.LayoutGravity(CommonPopupWindow.LayoutGravity.ALIGN_LEFT| CommonPopupWindow.LayoutGravity.TO_BOTTOM);
                layoutGravity.setHoriGravity(CommonPopupWindow.LayoutGravity.ALIGN_RIGHT);
                layoutGravity.setVertGravity(CommonPopupWindow.LayoutGravity.TO_BOTTOM);
                windowContent.showBashOfAnchor(a, layoutGravity, 0, 0);
                horiText.setText(context);
            }
        });

    }

    private void initPop() {
        windowContent = new CommonPopupWindow(this, R.layout.item_show_content, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) {
            @Override
            protected void initView() {
                View view=getContentView();
                horiText = view.findViewById(R.id.hori_text);
            }

            @Override
            protected void initEvent() {}
        };
    }

    private List<MMMDetailBean> mList = new ArrayList<>();

    @Override
    protected void initData() {
        HttpParams params = new HttpParams();
        params.put("oid", oid);
        OkGoUtils.getInstance().post(this, URL.GET_KANBAN_VIEW, params, 1, true, new MyCallback() {
            @Override
            public void onSuccess(ResponseBean response, int id) {
                if (200 == response.getCode()){
                    String dataInfo = response.getDataInfo().toString();
                    List<MMMDetailBean> infoBean = JSON.parseArray(dataInfo, MMMDetailBean.class);

                    if (infoBean.size() > 0){
                        MMMDetailBean mmmDetailBean = infoBean.get(0);
                        mTvDjbh.setText(mmmDetailBean.getCode());
                        mTvGz.setText(mmmDetailBean.getStation());
                        mTvDjzt.setText(mmmDetailBean.getState());
                    }

                    String detailInfo = response.getDetail().toString();
                    List<MMMDetailBean> mDetailBean = JSON.parseArray(detailInfo, MMMDetailBean.class);

                    for (MMMDetailBean bean : mDetailBean) {
                        mList.add(bean);
                    }

                   /* if (1 == type){
                        for (int i = 0; i < mList.size(); i++) {
                            mList.get(0).setOutNumber("");
                        }
                    }*/


                    mRecyclerAdapter.setData(mList);
                }
            }

            @Override
            public void onError(Throwable e, int id) {

            }
        });
    }
    private int whether = 0;
    public void onClickConfirm(View view) {
        List<MMMDetailBean> pollingBean = mRecyclerAdapter.getCheckItemsBean();
        for (int i = 0; i < pollingBean.size(); i++) {
            String outNumber = pollingBean.get(i).getOutNumber();
            if (!"".equals(outNumber)){
                whether = 1;
            }
        }

        if (whether == 0){
            showToast("请至少输入一个出库数");
            return;
        }

        for (int i = 0; i < pollingBean.size(); i++) {
            String outNumber = pollingBean.get(i).getOutNumber();
            if ("".equals(outNumber)){
               pollingBean.get(i).setOutNumber("0");
            }
        }

       // String userId = ToolUtils.getUserId(this);
        String tabJson = JSON.toJSONString(pollingBean);
        LogUtils.e("ab==="+tabJson);
        HttpParams params = new HttpParams();
        params.put("map", tabJson);
        params.put("userId", userId);
        OkGoUtils.getInstance().post(this, URL.GET_KANBAN_UP, params, 2, true, new MyCallback() {
            @Override
            public void onSuccess(ResponseBean response, int id) {
                if (200 == response.getCode()){
                    Intent intent = new Intent();
                    setResult(RESULT_OK, intent);
                    showToast("提交成功");
                    finish();
                }else {
                    showToast(response.getMsg());
                }
            }

            @Override
            public void onError(Throwable e, int id) {

            }
        });
    }

}
